


// Author:					HungNM
// Created:					16-08-2012
// Last Modified:			16-08-2012
//
	
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using eProject.Data;
	
namespace eProject.Business
{
	
	public class RolePermissions
	{

		#region Constructors

	    public RolePermissions()
		{}
	    
	
	    public RolePermissions(
			int roleId, 
			int permissionId) 
		{
	        GetRolePermissions(
				roleId, 
				permissionId); 
	    }

		#endregion

		#region Private Properties

		private int id = -1;
		private int roleId = -1;
		private int permissionId = -1;
		private DateTime createdDate = DateTime.UtcNow;
		private DateTime lastUpdate = DateTime.UtcNow;
		
		#endregion

		#region Public Properties

		public int Id 
		{
			get { return id; }
			set { id = value; }
		}
		public int RoleId 
		{
			get { return roleId; }
			set { roleId = value; }
		}
		public int PermissionId 
		{
			get { return permissionId; }
			set { permissionId = value; }
		}
		public DateTime CreatedDate 
		{
			get { return createdDate; }
			set { createdDate = value; }
		}
		public DateTime LastUpdate 
		{
			get { return lastUpdate; }
			set { lastUpdate = value; }
		}

		#endregion

		#region Private Methods

		/// <summary>
		/// Gets an instance of RolePermissions.
		/// </summary>
		/// <param name="roleId"> roleId </param>
		/// <param name="permissionId"> permissionId </param>
		private void GetRolePermissions(
			int roleId, 
			int permissionId) 
		{
			try
			{
				using(IDataReader reader = DBRolePermissions.GetOne(
					roleId, 
					permissionId)) 
				{
					PopulateFromReader(reader);
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		
		private void PopulateFromReader(IDataReader reader) 
		{
			try
			{
				if(reader.Read())
				{
					this.id = Convert.ToInt32(reader["Id"]);
					this.roleId = Convert.ToInt32(reader["RoleId"]);
					this.permissionId = Convert.ToInt32(reader["PermissionId"]);
					this.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					this.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
				
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		/// <summary>
        /// Persists a new instance of RolePermissions. Returns true on success.
        /// </summary>
        /// <returns></returns>
		private bool Create()
		{ 
			int newID = 0;
			
			try
			{
				newID = DBRolePermissions.Create(
					this.id, 
					this.createdDate, 
					this.lastUpdate); 
					
				this.roleId = newID;
				this.permissionId = newID;
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return (newID > 0);
		}

		
		/// <summary>
        /// Updates this instance of RolePermissions. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		private bool Update()
		{
			bool IsUpdated = false;
			try
			{
				IsUpdated = DBRolePermissions.Update(
					this.id, 
					this.roleId, 
					this.permissionId, 
					this.createdDate, 
					this.lastUpdate); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsUpdated;
		}

		#endregion

		#region Public Methods

		/// <summary>
        /// Saves this instance of RolePermissions. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		public bool Save()
		{
			bool IsSaved = false;
			try
			{
				if(this.roleId > 0)
				{
					IsSaved = Update();
				}
				else
				{
					IsSaved = Create();
				}
				if(this.permissionId > 0)
				{
					IsSaved = Update();
				}
				else
				{
					IsSaved = Create();
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsSaved;
		}
		
		#endregion

		#region Static Methods

		/// <summary>
		/// Deletes an instance of RolePermissions. Returns true on success.
		/// </summary>
		/// <param name="roleId"> roleId </param>
		/// <param name="permissionId"> permissionId </param>
		/// <returns>bool</returns>
		public static bool Delete(
			int  roleId, int  permissionId) 
		{
			bool IsDeleted = false;
			try
			{
				IsDeleted = DBRolePermissions.Delete(
					roleId, 
					permissionId); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsDeleted;
		}
		
		
		/// <summary>
		/// Gets a count of RolePermissions. 
		/// </summary>
		public static int GetCount()
		{
			int count = 0;
			try
			{
				count = DBRolePermissions.GetCount();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return count;
		}
	
		private static List<RolePermissions> LoadListFromReader(IDataReader reader)
		{
			List<RolePermissions> rolePermissionsList = new List<RolePermissions>();
			try
			{
				while (reader.Read())
				{
					RolePermissions rolePermissions = new RolePermissions();
					rolePermissions.id = Convert.ToInt32(reader["Id"]);
					rolePermissions.roleId = Convert.ToInt32(reader["RoleId"]);
					rolePermissions.permissionId = Convert.ToInt32(reader["PermissionId"]);
					rolePermissions.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					rolePermissions.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
					rolePermissionsList.Add(rolePermissions);
					
				}
			}
			finally
			{
				reader.Close();
			}
	
			return rolePermissionsList;
		}
	
		/// <summary>
		/// Gets an IList with all instances of RolePermissions.
		/// </summary>
		public static List<RolePermissions> GetAll()
		{
			List<RolePermissions> listRolePermissions = new List<RolePermissions>();
			try
			{
				IDataReader reader = DBRolePermissions.GetAll();
				listRolePermissions = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listRolePermissions;
		}
	
		/// <summary>
		/// Gets an IList with page of instances of RolePermissions.
		/// </summary>
		/// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
		public static List<RolePermissions> GetPage(int pageNumber, int pageSize, out int totalPages)
		{
			totalPages = 1;
			List<RolePermissions> listRolePermissions = new List<RolePermissions>();
			try
			{
				IDataReader reader = DBRolePermissions.GetPage(pageNumber, pageSize, out totalPages);	
				listRolePermissions = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listRolePermissions;
		}
		
		#endregion

	}
}





